
$.validator.setDefaults({
    submitHandler: function () {
        alert("submitted!");
    }
});

$().ready(function () {
    // validate signup form on keyup and submit
    $("#signupForm").validate({
        rules: {
            u: {
                required: true,
                minlength: 3
            },
            p: {
                required: true,
                minlength: 5
            },
            cp: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            email: {
                minlength: 2,
                required: true,
                email: true
            }
        },
        messages: {
            u: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 3 characters"
            },
            p: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            cp: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            },
            email: "Please enter a valid email address"
        }
    });

    // newsletter topics are optional, hide at first
    var inital = newsletter.is(":checked");
    var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
    var topicInputs = topics.find("input").attr("disabled", !inital);
    // show when newsletter is checked
    newsletter.click(function () {
        topics[this.checked ? "removeClass" : "addClass"]("gray");
        topicInputs.attr("disabled", !this.checked);
    });
});